package com.example.ccj.b.c.custom.selectuser;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.StringUtils;

import java.util.List;
import java.util.Map;

import com.example.ccj.R;

/**
 * 选择人员
 * 2024年11月17日23:42:03
 */
public class AllSelectUserAdapter extends RecyclerView.Adapter<AllSelectUserAdapter.ViewHolder>{
    ISelectUserListener iSelectUserListener;
    List<UserMode> userModeList;
    List<UserMode> selectedUserList;
    Map<Long,UserMode> selectedUserMap;
    /**
     * 字段类型数组
     *
     * @param userModeList 全部人员列表
     * @param selectedUserMap 已选择的人员列表
     * @param iSelectUserListener 通过此回调返回选择的字段编号
     */
    public AllSelectUserAdapter(List<UserMode> userModeList,List<UserMode> selectedUserList, Map<Long,UserMode> selectedUserMap, @NonNull ISelectUserListener iSelectUserListener){
        this.userModeList = userModeList;
        this.iSelectUserListener = iSelectUserListener;
        this.selectedUserList = selectedUserList;
        this.selectedUserMap = selectedUserMap;
    }

    @NonNull
    @Override
    public AllSelectUserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AllSelectUserAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).
                inflate(R.layout.adapater_select_user, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AllSelectUserAdapter.ViewHolder holder, int position) {
        UserMode userMode = userModeList.get(position);
        holder.checkBox.setOnCheckedChangeListener(null);
        if (selectedUserMap.containsKey(userMode.id)) holder.checkBox.setChecked(true);
        else holder.checkBox.setChecked(false);
        holder.name.setText(userMode.name);
        if (!StringUtils.isEmpty(userMode.icon)) ImageUtils.loadImage(holder.icon.getContext(),userMode.icon,holder.icon);
        else holder.icon.setImageResource(R.mipmap.select_user_icon);


        final int po = position;
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (iSelectUserListener != null) iSelectUserListener.add(userMode.id,userMode);
                } else {
                    if (iSelectUserListener != null) iSelectUserListener.delete(userMode.id,userMode);
                }
            }
        });

        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.checkBox.setChecked(!holder.checkBox.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return userModeList == null ? 0 : userModeList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final CheckBox checkBox;
        private final ImageView icon;
        private final TextView name;
        private final LinearLayout ll;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.adapater_select_user_name);
            checkBox = itemView.findViewById(R.id.adapater_select_user_check_box);
            icon = itemView.findViewById(R.id.adapater_select_user_icon);
            ll = itemView.findViewById(R.id.adapater_select_user_ll);
        }
    }
}
